<template>
  <div style="padding: 15px">
    <div style="margin-bottom: 15px;">
      <Input style="width: 200px;" v-model="searchValue" placeholder="请输用户名称"/>
      <Select v-model="searchKey" filterable style="width:200px">
        <Option v-for="item in storeList" :value="item.id" :key="item.id">{{ item.storeName }}</Option>
      </Select>
      <Button type="success">搜索</Button>
      <Button @click="add" type="primary" style="float: right;">添加用户</Button>
    </div>
    <Table border :columns="userTitle" :data="userList"></Table>
    <page-com :total="total" @page-change="pageChange"></page-com>
    <Modal v-model="identity" :title="modalTitle" :width="750" @on-ok="submit" @on-cancel="cancel" :mask-closable="false">
      <Form :model="addForm" :label-width="80">
        <FormItem label="用户账号">
          <Input v-model="addForm.username" placeholder="请输入账号" />
        </FormItem>
        <FormItem label="真实姓名">
          <Input v-model="addForm.nickName" placeholder="请输入真实姓名" />
        </FormItem>
        <FormItem label="联系方式">
          <Input v-model="addForm.phone" placeholder="请输入联系方式" />
        </FormItem>
        <FormItem label="注册书屋">
          <Select v-model="addForm.storeeId" filterable label-in-value @on-change="storeChange">
            <Option v-for="item in storeList" :value="item.id" :key="item.id">{{ item.storeName }}</Option>
          </Select>
        </FormItem>
        <FormItem label="设置角色">
          <Select v-model="addForm.roleId">
            <Option v-for="item in roleList" :value="item.roleId" filterable :key="item.roleId">{{ item.roleName }}</Option>
          </Select>
        </FormItem>
        <FormItem label="设置禁启">
          <i-switch size="large" v-model="addForm.state">
            <span slot="open">启用</span>
            <span slot="close">禁用</span>
          </i-switch>
        </FormItem>
      </Form>
    </Modal>
  </div>
</template>

<script>
  import PageCom from '_c/page'
  export default {
    name: 'users',
    data () {
      return {
        searchValue: '',
        searchKey: '',
        modalTitle: '',
        identity: false,
        number: 0,
        total: 0,
        size: 10,
        addForm: {
          state: true
        },
        userTitle: [
          {
            title: '用户账号',
            align: 'center',
            tooltip: true,
            key: 'username'
          },
          {
            title: '真实姓名',
            align: 'center',
            tooltip: true,
            key: 'nickName'
          },
          {
            title: '联系方式',
            align: 'center',
            tooltip: true,
            key: 'phone'
          },
          {
            title: '所属门店',
            align: 'center',
            tooltip: true,
            key: 'storeeName'
          },
          {
            title: '角色',
            align: 'center',
            tooltip: true,
            render: (h, params) => {
              return h('div', params.row.roles[0].roleName)
            }
          },
          {
            title: '添加时间',
            align: 'center',
            tooltip: true,
            render: (h, params) => {
              return h('div', this.$formatDate(new Date(params.row.createTime), 'yyyy-MM-dd hh:mm'))
            }
          },
          {
            title: '状态',
            align: 'center',
            render: (h, params) => {
              const row = params.row
              const color = row.state === 0 ? 'primary' : row.state === 1 ? 'error' : ''
              const text = row.state === 0 ? '正常' : row.state === 1 ? '禁用' : ''
              return h('Tag', {
                props: {
                  type: 'dot',
                  color: color
                }
              }, text)
            }
          },
          {
            title: '操作',
            key: 'action',
            width: 240,
            align: 'center',
            render: (h, params) => {
              return h('div', [
                h('Button', {
                  props: {
                    type: 'warning',
                    size: 'small'
                  },
                  style: {
                    marginRight: '5px'
                  },
                  on: {
                    click: () => {
                      this.$Modal.confirm({
                        title: '你确定要重置密码？',
                        okText: '确定',
                        cancelText: '取消',
                        onOk: () => {
                          this.resetPassword(params.row.userId)
                        }
                      })
                    }
                  }
                }, '重置密码'),
                h('Button', {
                  props: {
                    type: 'primary',
                    size: 'small'
                  },
                  style: {
                    marginRight: '5px'
                  },
                  on: {
                    click: () => {
                      this.editor()
                      let row = params.row
                      this.addForm.state = Boolean(row.state === 1 ? 0 : 1)
                      this.addForm.username = row.username
                      this.addForm.nickName = row.nickName
                      this.addForm.phone = row.phone
                      this.addForm.storeeId = row.storeeId
                      this.addForm.storeeName = row.storeeName
                      this.addForm.roleId = row.roles[0].roleId
                      this.addForm.userId = row.userId
                    }
                  }
                }, '编辑'),
                h('Button', {
                  props: {
                    type: 'error',
                    size: 'small'
                  },
                  on: {
                    click: () => {
                      this.$Modal.confirm({
                        title: '你确定删除该标签吗？',
                        okText: '确定',
                        cancelText: '取消',
                        onOk: () => {
                          this.remove(params.row.userId)
                        }
                      })
                    }
                  }
                }, '删除')
              ])
            }
          }
        ],
        userList: [],
        storeList: [],
        roleList: []
      }
    },
    mounted() {
      this.userPageList()
      this.searchStoreList()
      this.rolePage()
    },
    methods: {
      searchStoreList() {
        let url = '/api/storee'
        this.$get(url).then((res) => {
          this.storeList = res
        })
      },
      rolePage() {
        let url = '/api/role'
        this.$get(url).then((res) => {
          if (res.code === 0) {
            this.roleList = res.data
          }
        })
      },
      userPageList() {
        let url = '/api/user/query'
        this.$post(url, this.$qs.stringify({
          page: this.number,
          limit: this.size,
          searchValue: this.searchValue
        })).then((res) => {
          this.userList = res.data
          this.total = res.count
        })
      },
      title(title) {
        this.modalTitle = title
        this.identity = true
      },
      add() {
        this.title('添加用户')
      },
      editor() {
        this.title('编辑用户')
      },
      submit() {
        if (this.modalTitle === '添加用户') {
          this.addOrg()
        } else {
          this.theEditor()
        }
      },
      addOrg() {
        this.addForm.state = Number(!this.addForm.state)
        this.$post('/api/user', this.$qs.stringify(this.addForm)).then((res) => {
          if (res.code === 200) {
            this.$Message.success('添加成功')
            this.userPageList()
            this.cancel()
          } else {
            this.$Message.error('添加失败')
          }
        }).catch((rel) => {
          this.$Message.error('添加失败')
        })
      },
      theEditor() {
        this.addForm.state = Number(!this.addForm.state)
        this.$put('/api/user', this.$qs.stringify(this.addForm)).then((res) => {
          if (res.code === 200) {
            this.$Message.success('修改成功')
            this.userPageList()
            this.cancel()
          } else {
            this.$Message.error('修改失败')
          }
        }).catch((rel) => {
          this.$Message.error('修改失败')
        })
      },
      resetPassword(id) {
        let url = `/api/user/psw/${id}`
        this.$put(url, this.$qs.stringify({userId: id})).then((res) => {
          if (res.code === 200) {
            this.$Message.success('重置成功')
          } else {
            this.$Message.error('重置失败')
          }
        }).catch((rel) => {
          this.$Message.error('重置失败')
        })
      },
      remove(id) {
        let url = '/api/user/state'
        this.$put(url, this.$qs.stringify({userId: id})).then((res) => {
          console.log(res)
        })
      },
      searchOrg () {
        this.userPageList()
      },
      cancel() {
        this.addForm = {}
      },
      pageChange(data) {
        this.number = data
        this.userPageList()
      },
      storeChange(e) {
        this.addForm.storeeId = e.value
        this.addForm.storeeName = e.label
      }
    },
    components: {
      PageCom
    }
  }
</script>

<style scoped>

</style>

